$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'statistics/bstatisticspmtwo/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true , hidden: true},
			{ label: 'PM2.5 ', name: 'value', index: 'value', width: 80 ,align: "center"}, 			
			{ label: '单位', name: 'unit', index: 'unit', width: 80 ,align: "center"}, 			
			{ label: '机器id', name: 'machineId', index: 'machine_id', width: 80 ,align: "center"}, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,
                align: "center",
                formatter: function (value, options, row) {
                    return new Date(value).Format('yyyy-MM-dd HH:mm:ss');
             }}, 			
             { label: '使用状态', name: 'isUsed', index: 'is_used', width: 80 ,
                 align: "center",
                 formatter: function (value, options, row) {
                     return value === 1 ?
                         '<span class="label label-success" value="1">使用</span>' :
                         '<span class="label label-danger" value="0">未使用</span>';
            }}				
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "statisticsPmTwoList.list",
            page: "statisticsPmTwoList.pageNum",
            total: "statisticsPmTwoList.pages",
            records: "statisticsPmTwoList.total"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bStatisticsPmTwo: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bStatisticsPmTwo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bStatisticsPmTwo.id == null ? "statistics/bstatisticspmtwo/save" : "statistics/bstatisticspmtwo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bStatisticsPmTwo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "statistics/bstatisticspmtwo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "statistics/bstatisticspmtwo/info/"+id, function(r){
                vm.bStatisticsPmTwo = r.bStatisticsPmTwo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}